﻿import pygame
from AnimationSheet import *
from SimpleButton import *
from SimpleSprite import *
from pygame.locals import *

class SelectPart:
	def __init__(self, game_app, py_screen):
		print "select part create"
		self.screen = py_screen
		self.ws = game_app
		self.ws.SetArrowCursor()
		self.select_part_caption = self.ws.test_font.render("This is Select Game Part", False, (255,255,255))
		self.windows_bgcolor = (0,0,0)#(127,127,63)
		self.mouse_pos = (0,0)
		self.menu_exit_over = pygame.image.load("data/graphics/buttons/back_btn_over.png")#self.ws.test_font.render("Exit",False,(64,64,64))
		self.menu_exit_up = pygame.image.load("data/graphics/buttons/back_btn_up.png")
		self.menu_exit = self.menu_exit_up
		self.menu_exit_rect = pygame.Rect(290,425, self.menu_exit.get_width(), self.menu_exit.get_height());
		self.menu_exit_is_over = False
		
		self.part_close = pygame.image.load("data/graphics/part_close.png")
		self.part_close = self.part_close.convert_alpha(self.part_close)
		
		self.part_1 = pygame.image.load("data/graphics/part_1.png")
		self.part_1 = self.part_1.convert(self.part_1)
		if not self.ws.part_1_open:
			self.TinImage(self.part_1)
		print "image 1 load"
		
		self.part_2 = pygame.image.load("data/graphics/part_2.png")
		self.part_2 = self.part_2.convert(self.part_2)
		if not self.ws.part_2_open:
			self.TinImage(self.part_2)
		print "image 2 load"	
			
		self.part_3 = pygame.image.load("data/graphics/part_3.png")
		self.part_3 = self.part_3.convert(self.part_3)
		if not self.ws.part_3_open:
			self.TinImage(self.part_3)
		print "image 1 load"

		self.part_select = pygame.image.load("data/graphics/part_select.png");
		self.part_select = self.part_select.convert_alpha(self.part_select)
		self.show_select = False
		
		self.part_1_rect = pygame.Rect(30,200, self.part_1.get_width(), self.part_1.get_height());
		self.part_2_rect = pygame.Rect(210,200, self.part_2.get_width(), self.part_2.get_height());
		self.part_3_rect = pygame.Rect(390,200, self.part_3.get_width(), self.part_3.get_height());
		
		self.select_pos = (0,0)
		
		
		#задний фон
		self.bg_image = pygame.image.load("data/graphics/bgs/bg_1.png").convert()
		
		#заголовок сцены
		self.caption_image = pygame.image.load("data/graphics/captions/select_part.png").convert_alpha()
		self.caption_pos = ((640 - self.caption_image.get_width()) / 2, 20)
		
		#анимированная стрелка
		self.anim_arrow_img = pygame.image.load("data/graphics/sheets/up_arrow.png").convert_alpha()
		self.anim_arrow = AnimationSheet(self.anim_arrow_img, 61, 66, self.screen, self.ws)
		
		#первая часть
		self.part_1_img = pygame.image.load("data/graphics/elements/part_1.png").convert_alpha()
		self.part_1_pos = Rect(18,85, 10,10)
		self.part_1_mask = pygame.mask.from_surface(self.part_1_img)
		
		self.part_1_btn = SimpleButton(self.part_1_pos,[self.part_1_img], self.screen)
		
		
		#вторая часть
		self.part_2_img = pygame.image.load("data/graphics/elements/part_2.png").convert_alpha()
		self.part_2_pos = Rect(206,95, 10, 10)
		self.part_2_mask = pygame.mask.from_surface(self.part_2_img)
		
		self.part_2_btn = SimpleButton(self.part_2_pos,[self.part_2_img], self.screen)
		
		
		#третья часть
		self.part_3_img = pygame.image.load("data/graphics/elements/part_3.png").convert_alpha()
		self.part_3_pos = Rect(420,50, 10, 10)
		self.part_3_mask = pygame.mask.from_surface(self.part_3_img)
		
		self.part_3_btn = SimpleButton(self.part_3_pos,[self.part_3_img], self.screen)
		
		
		#маски частей
		self.mask_1 = pygame.Mask((640,480))
		self.mask_1.draw(self.part_1_mask, self.part_1_pos.topleft)
		
		self.mask_2 = pygame.Mask((640,480))
		self.mask_2.draw(self.part_2_mask, self.part_2_pos.topleft)
		
		self.mask_3 = pygame.Mask((640,480))
		self.mask_3.draw(self.part_3_mask, self.part_3_pos.topleft)
		
		self.part_1_btn.SetMask(self.mask_1)
		self.part_2_btn.SetMask(self.mask_2)
		self.part_3_btn.SetMask(self.mask_3)
		
		self.arrow_pos = ((88, 336), (288, 336), (508, 336))
		
		self.anim_arrow.SetPos(self.arrow_pos[0])
		
		self.btns = [self.part_1_btn, self.part_2_btn, self.part_3_btn]
		
		self.lock_pos = ((288, 170), (488, 170))
		
		self.lock_img = pygame.image.load("data/graphics/elements/locker.png").convert_alpha()
		
		self.locks = []
		
		if not self.ws.part_2_open:
			self.locks.append(SimpleSprite(self.lock_pos[0], self.lock_img, self.screen))
		if not self.ws.part_3_open:
			self.locks.append(SimpleSprite(self.lock_pos[1], self.lock_img, self.screen))
		
	def TinImage(self, img):
		#img.blit(self.part_close, (img.get_width() / 2 - self.part_close.get_width() / 2, 0)) 
		
		PALETTE = img.get_palette()
		for i in range(0,256):
			PALETTE[i][0] = int(PALETTE[i][0] / 2)
			PALETTE[i][1] = int(PALETTE[i][1] / 2)
			PALETTE[i][2] = int(PALETTE[i][2] / 2)
		#PALETTE -= FADE_STEP
		img.set_palette(PALETTE)
		
	
		
	def Update(self):
		for event in pygame.event.get():
			if event.type == QUIT:
				print "esc down"
				self.ws.mainLoop = False
			if event.type == MOUSEBUTTONUP:
				m_pos = event.pos
				if self.menu_exit_rect.collidepoint(m_pos):
					self.ws.SetCurrentScene(2)
				if self.part_1_btn.mask.get_at(m_pos) != 0:
					self.ws.SetCurrentScene(5)
					
		self.mouse_pos = pygame.mouse.get_pos()
		
		if self.menu_exit_rect.collidepoint(self.mouse_pos):
			if not self.menu_exit_is_over:
				self.menu_exit_is_over = True
				self.menu_exit = self.menu_exit_over
		elif self.menu_exit_is_over:
			self.menu_exit_is_over = False
			self.menu_exit = self.menu_exit_up
			
		"""if self.part_1_rect.collidepoint(self.mouse_pos):
			if self.ws.part_1_open:
				self.show_select = True
			else:
				self.show_select = False
			self.select_pos = (self.part_1_rect.left,self.part_1_rect.top + self.part_1_rect.height)
			#####
			self.anim_arrow.SetPos(self.arrow_pos[0])
		elif self.part_2_rect.collidepoint(self.mouse_pos):
			if self.ws.part_2_open:
				self.show_select = True
			else:
				self.show_select = False
			self.select_pos = (self.part_2_rect.left,self.part_2_rect.top + self.part_2_rect.height)
			#####
			self.anim_arrow.SetPos(self.arrow_pos[1])
			
		elif self.part_3_rect.collidepoint(self.mouse_pos):
			if self.ws.part_3_open:
				self.show_select = True
			else:
				self.show_select = False
			self.select_pos = (self.part_3_rect.left,self.part_3_rect.top + self.part_3_rect.height)
			#####
			self.anim_arrow.SetPos(self.arrow_pos[2])
		else:
			self.show_select = False"""

		#self.screen.fill(self.windows_bgcolor)
		
		
		if self.part_1_btn.MaskOver(self.mouse_pos):
			if self.ws.part_1_open:
				self.anim_arrow.SetPos(self.arrow_pos[0])
		elif self.part_2_btn.MaskOver(self.mouse_pos):
			if self.ws.part_2_open:
				self.anim_arrow.SetPos(self.arrow_pos[1])
		elif self.part_3_btn.MaskOver(self.mouse_pos):
			if self.ws.part_3_open:
				self.anim_arrow.SetPos(self.arrow_pos[2])
		
		#рисуем задний фон
		self.screen.blit(self.bg_image,(0,0))
		
		#рисуем курсор
		pygame.draw.circle(self.screen, (63,63,127), self.mouse_pos, 20)
		
		#рисуем заголовок
		self.screen.blit(self.caption_image, self.caption_pos)
		
		"""#рисуем первую часть
		self.screen.blit(self.part_1_img, self.part_1_pos)
		#рисуем вторую часть
		self.screen.blit(self.part_2_img, self.part_2_pos)
		#рисуем третью часть
		self.screen.blit(self.part_3_img, self.part_3_pos)"""
		
		
		
		for btn in self.btns:
			btn.Draw()
			
		for lock in self.locks:
			lock.Draw()
		
		
		self.anim_arrow.Draw()
		
		#self.screen.blit(self.select_part_caption, (200,50))
		
		
		
		#pygame.draw.rect(self.screen, (128,128,128), self.menu_exit_rect)
		self.screen.blit(self.menu_exit, self.menu_exit_rect.topleft)
		
		pygame.display.update()